﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BookMS
{
    public partial class admin21 : Form
    {
        public admin21()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(textBox1.Text!=""&& textBox2.Text != "" && textBox3.Text != "" && textBox4.Text != "" && textBox5.Text != "")
            {
                Dao dao = new Dao();
                string str = $"insert into book values('{textBox1.Text}','{textBox2.Text}','{textBox3.Text}','{textBox4.Text}',{textBox5.Text})";
                try
                {
                    int n = dao.Execute(str);
                    if (n > 0)
                    {
                        MessageBox.Show("添加成功！");
                    }
                    else
                    {
                        MessageBox.Show("添加失败！");
                    }
                }
                catch (MySql.Data.MySqlClient.MySqlException ex)
                {
                    // 检查是否是主键重复的异常
                    if (ex.Number == 1062)  // 1062: Duplicate entry for key 'PRIMARY'
                    {
                        MessageBox.Show("添加失败：书号已经存在！");
                    }
                    else
                    {
                        MessageBox.Show($"请输入正确的数据！");
                    }
                }

                // 清空所有文本框
                foreach (Control ctrl in this.Controls)
                {
                    if (ctrl is TextBox)
                    {
                        TextBox tb = (TextBox)ctrl;
                        tb.Text = "";
                    }
                }

                dao.DaoClose();
            }
            else
            {
                MessageBox.Show("输入不允许有空！");
            }
        }


        private void button2_Click(object sender, EventArgs e)
        {
            foreach (Control ctrl in this.Controls)
            {
                if (ctrl is TextBox)
                {
                    TextBox tb = (TextBox)ctrl;
                    tb.Text = "";
                }
            }
        }
    }
}
